# 机器学习进阶-图像形态学变化-礼帽与黑帽 1.cv2.TOPHAT(礼帽-原始图片-开运算后图片) 2.cv2.BLACKHAT(黑帽 闭运算-原始图片)
# https://www.cnblogs.com/my-love-is-python/p/10395237.html
# 图像开运算、闭运算、形态学梯度、“礼帽”和“黑帽”
# https://blog.csdn.net/hujian_/article/details/51078651
import cv2
import matplotlib.pyplot as plt
import numpy as np

# 1.读取图片
img = cv2.imread('dige.png')

# 2. 使用cv2.MORPH_TOPHAT获得礼帽图片
kernel = np.ones((3, 3), np.uint8)
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)

# 3. 使用cv2.MORPH_BLACKHAT获得黑帽图片
kernel = np.ones((3, 3), np.uint8)
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)

# 5. 绘制两者的图片
images = [img, tophat, blackhat]
titles = ['IMG', 'TOPHAT', 'BLACKHAT']

for i in range(3):
    plt.subplot(2, 3, i + 1)
    plt.imshow(images[i])
    plt.title(titles[i])
plt.show()
